package com.fy.fyspace.service.hotness.strategy.impl;

import com.fy.fyspace.model.dto.hotness.HotnessCalculationDTO;
import com.fy.fyspace.service.hotness.strategy.HotnessCalculationStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 简单热度计算策略实现
 * 设计模式：策略模式的具体实现
 * 作用：实现简单的热度计算算法，不包含时间衰减因子
 * 
 * @author fwq
 * @since 2024-12-19
 */
@Slf4j
@Component("simpleHotnessStrategy")
public class SimpleHotnessStrategy implements HotnessCalculationStrategy {
    
    // 简单权重配置
    private static final BigDecimal DOWNLOAD_WEIGHT = new BigDecimal("1.0");
    private static final BigDecimal LIKE_WEIGHT = new BigDecimal("1.0");
    private static final BigDecimal FAVORITE_WEIGHT = new BigDecimal("2.0");
    
    @Override
    public BigDecimal calculateHotness(HotnessCalculationDTO calculationDTO) {
        try {
            // 简单计算：下载数 + 点赞数 + 收藏数*2
            BigDecimal downloadScore = DOWNLOAD_WEIGHT.multiply(new BigDecimal(calculationDTO.getDownloadCount()));
            BigDecimal likeScore = LIKE_WEIGHT.multiply(new BigDecimal(calculationDTO.getLikeCount()));
            BigDecimal favoriteScore = FAVORITE_WEIGHT.multiply(new BigDecimal(calculationDTO.getFavoriteCount()));
            
            // 计算总分
            BigDecimal totalScore = downloadScore.add(likeScore).add(favoriteScore);
            
            // 保留两位小数
            return totalScore.setScale(2, RoundingMode.HALF_UP);
            
        } catch (Exception e) {
            log.error("计算热度分数时发生错误，图片ID: {}", calculationDTO.getPictureId(), e);
            return BigDecimal.ZERO;
        }
    }
    
    @Override
    public String getStrategyName() {
        return "SimpleHotnessStrategy";
    }
    
    @Override
    public String getStrategyDescription() {
        return "简单热度计算策略：基于下载、点赞、收藏的简单加权计算，不包含时间衰减";
    }
    
    @Override
    public String getStrategyType() {
        return "SIMPLE";
    }
    
    @Override
    public boolean isEnabled() {
        return true;
    }
} 